package com.feicheng.controller.shop.admin.client;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.model.ClientDepositPlus;
import com.feicheng.persistence.model.CutDepositRecordMain;
import com.feicheng.persistence.model.DepositRecordMain;
import com.feicheng.persistence.service.IDepositRecordService;
import com.feicheng.service.IFClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController("/shop/admin/client/deposit-record")
@RequestMapping("/shop/admin/client/deposit-record")
public class DepositRecordController {

    @Autowired
    IFClientService fClientService;

    @Autowired
    IDepositRecordService depositRecordService;

    /**
     * 分页查询押金记录V2
     * @return
     */
    @GetMapping("/list/v2")
    public ResultInfo getListV2(
            ClientDepositPlus clientDepositPlus,
            @RequestParam(defaultValue="1") Integer current,
            @RequestParam(defaultValue="10")Integer size
    ){

        QueryWrapper<ClientDepositPlus> clientDepositPlusWrapper = new QueryWrapper<>();

        if (null != clientDepositPlus.getStoreId()) {
            clientDepositPlusWrapper.eq("store_id", clientDepositPlus.getStoreId());
        }

        if (null != clientDepositPlus.getClientName()) {
            clientDepositPlusWrapper.like("client_name", clientDepositPlus.getClientName());
        }

        if (null != clientDepositPlus.getOriginalCode() && clientDepositPlus.getOriginalCode() != ""){
            clientDepositPlusWrapper.like("original_code",clientDepositPlus.getOriginalCode());
        }

        if (null != clientDepositPlus.getStatu() && clientDepositPlus.getStatu() != "") {
            clientDepositPlusWrapper.like("statu", clientDepositPlus.getStatu());
        }

        clientDepositPlusWrapper.isNotNull(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE);

        clientDepositPlusWrapper.eq(Constant.ENTITY.DEPOSIT_RECORD.TYPE, Constant.VAL.DEPOSIT.INS);

        clientDepositPlusWrapper.orderByDesc("statu", "ct");

        Page<ClientDepositPlus> page = new Page<ClientDepositPlus>(current, size);

        return ResultInfo.success(depositRecordService.getClientDepositPlusByPage(page, clientDepositPlusWrapper));
    }

    /**
     * 分页查询押金记录
     * @param depositRecordMain
     * @param currentPage
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ResultInfo getList(
            DepositRecordMain depositRecordMain,
            @RequestParam(defaultValue="1") Integer currentPage,
            @RequestParam(defaultValue="10")Integer size
    ){
        return fClientService.getDepositRecordByPage(depositRecordMain, currentPage, size);
    }

    @GetMapping("/details")//取得押金记录
    public ResultInfo getDepositRecordList(Integer depositId){
        return fClientService.getDepositRecord(depositId);
    }

    //门店分页查询扣押记录列表
    @GetMapping("/cut/list")
    public ResultInfo getcutDepositRecord(
            String clientName,
            String ct,
            String lastct,
            String name,
            String ioFlg,
            String account,
            Integer storeId,
            Integer cutId,
            @RequestParam(defaultValue="1") Integer currentPage,
            @RequestParam(defaultValue="10")Integer size
    ){
        CutDepositRecordMain cutDepositRecordMain = new CutDepositRecordMain();
        if (null != ct){
            LocalDateTime ctTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(ct)), ZoneId.systemDefault());
            cutDepositRecordMain.setCt(ctTime);
        }
        if (null != lastct){
            LocalDateTime lastctTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(lastct)), ZoneId.systemDefault());
            cutDepositRecordMain.setLastct(lastctTime);
        }
        JSONObject result = new JSONObject();
        result.put(Constant.ENTITY.CUT_DEPOSIT_RECORD.CUT_ID, cutId);
        result.put(Constant.ENTITY.STORE.STORE_ID, storeId);
        result.put(Constant.ENTITY.CLIENT.CLIENT_NAME,clientName);
        result.put(Constant.ENTITY.CUT_DEPOSIT_RECORD.CT,cutDepositRecordMain.getCt());
        result.put(Constant.HTTP.PARAM.LASTCT,cutDepositRecordMain.getLastct());
        result.put(Constant.ENTITY.STORE.NAME,name);
        result.put(Constant.ENTITY.CUT_DEPOSIT_RECORD.IO_FLG,ioFlg);
        result.put(Constant.ENTITY.USER.ACCOUNT,account);
        result.put(Constant.HTTP.PARAM.CURRENT,currentPage);
        result.put(Constant.HTTP.PARAM.SIZE,size);
        return  fClientService.getCutDepositRecordByPage(result);
    }

    @GetMapping("/cut/info") //取得扣押记录详情
    public ResultInfo getCutDepositRecord(Integer cutId){
        return fClientService.getCutDepositRecord(cutId);
    }

}
